package com.john.permission.service.impl;

import com.john.permission.mapper.RoleMapper;
import com.john.permission.mapper.UserRoleMapper;
import com.john.permission.pojo.*;
import com.john.permission.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 查询用户所有角色
     * @param userInDB
     * @return
     */
    @Override
    public List<Role> getRoleInfo(User userInDB) {

        List<Role> roleList = new ArrayList<>();

        UserRoleExample userRoleExample = new UserRoleExample();
        userRoleExample.createCriteria().andUIdEqualTo(userInDB.getId());
        List<UserRoleKey> userRoles = userRoleMapper.selectByExample(userRoleExample);
        for(UserRoleKey userRole : userRoles) {
            RoleExample roleExample = new RoleExample();
            roleExample.createCriteria().andIdEqualTo(userRole.getrId());
            roleList.add(roleMapper.selectByPrimaryKey(userRole.getrId()));
        }
        return roleList;
    }
}
